package com.random.fox.whatspay.ui;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gc.materialdesign.views.LayoutRipple;
import com.nineoldandroids.view.ViewHelper;
import com.random.fox.whatspay.R;

/**
 * Created by 渠 on 2014/9/10.
 */
public class MenuFragment extends ListFragment {
    private String TAG = ((Object)this).getClass().getSimpleName();
    String[] options = new String[]{
            "概述","账户","明细","报表","设置"
    };
    int[] pics = new int[]{R.drawable.ic_home_48px,R.drawable.ic_face_48px,
    R.drawable.ic_assignment_48px,R.drawable.ic_equalizer_48px,R.drawable.ic_settings_48px};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MAdapter());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setDivider(new BitmapDrawable());
    }

    class MAdapter extends BaseAdapter{
        int length = 0;
        public MAdapter(){
            length = options.length;
        }
        private int selected;

        @Override
        public int getCount() {
            return length;
        }

        @Override
        public Object getItem(int i) {
            return options[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            LayoutRipple root = (LayoutRipple) View.inflate(getActivity(), R.layout.menu_item, null);
            setOriginRiple(root);
            root.setRippleSpeed(20); //默认速度是10f
            TextView menuName = (TextView) root.findViewById(R.id.tv_menu_name);
            menuName.setText(options[i]);
            ImageView icon = (ImageView) root.findViewById(R.id.iv_menu_icon);
            icon.setImageResource(pics[i]);
            if (i == selected){
                root.setBackgroundResource(android.R.color.darker_gray);
            }else {
                root.setBackgroundResource(android.R.color.transparent);
            }
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selected = i;
                    view.setBackgroundResource(android.R.color.darker_gray);
                    notifyDataSetChanged();
                    setSelection(i);
                    ((MainAty)getActivity()).showContent(i,null);
                }
            });
            return root;
        }

        private void setOriginRiple(final LayoutRipple layoutRipple){

            layoutRipple.post(new Runnable() {

                @Override
                public void run() {
                    View v = layoutRipple.getChildAt(0);
                    layoutRipple.setxRippleOrigin(ViewHelper.getX(v)+v.getWidth()/2);
                    layoutRipple.setyRippleOrigin(ViewHelper.getY(v)+v.getHeight()/2);

                    layoutRipple.setRippleColor(Color.parseColor("#1E88E5"));

                    layoutRipple.setRippleSpeed(30);
                }
            });

        }

    }
}
